package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsCustomerStatisticMapper;
import cc.rengu.igas.bsps.common.entity.CustomerStatisticInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wpf
 * @version 1.0
 * @description
 * @Date 2023/12/18 10:58
 */
public class BspsCustomerStatisticMapperImpl implements BspsCustomerStatisticMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public List<CustomerStatisticInfo> selectCustomerInfoByStatisticType(String mchntNo,String instId,String statisticType,String statisticDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCustomerInfoByStatisticType");
        CustomerStatisticInfo customerStatisticInfo = new CustomerStatisticInfo();
        customerStatisticInfo.setMchntNo(mchntNo);
        customerStatisticInfo.setInstId(instId);
        customerStatisticInfo.setStatisticType(statisticType);
        customerStatisticInfo.setStatisticDate(statisticDate);
        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, customerStatisticInfo, CustomerStatisticInfo.class);
        if (null == objects) {
            rglog.error("顾客交易统计数据查询失败");
            return null;
        }
        List<CustomerStatisticInfo> customerStatisticInfos = objects.stream().map(info -> (CustomerStatisticInfo) info).collect(Collectors.toList());
        return customerStatisticInfos;
    }

    @Override
    public int insertCustomerInfo(CustomerStatisticInfo customerStatisticInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTPCustomerInfo");
        int i = dbsUtil.dbsExecuteSql(sqlStr, customerStatisticInfo, null);
        if (i!=0) {
            rglog.error("顾客交易统计数据插入失败");
        }
        return i;
    }
}
